﻿using Bridge.Shared.Models;
using Bridge.System.Domain;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Roles;
using StackExchange.Redis;
using SysConfigs;
using SysConfigs.Input;
using SysConfigs.Output;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Uow;

namespace Bridge.System.HttpApi
{
    [Route("[controller]/[action]")]
    public class TestController:AbpController
    {
        private IRepository<SysConfig, Guid> _sysConfigRepository;
        private readonly IRoleService _roleService;
        private IRepository<Menu, Guid> _menuRepository;
        public TestController(IRepository<SysConfig, Guid> sysConfigRepository, IRoleService roleService, IRepository<Menu, Guid> menuRepository)
        {
            _sysConfigRepository = sysConfigRepository;
            _roleService = roleService;
            _menuRepository = menuRepository;
        }
        /// <summary>
        /// 测试连接沲连接不关
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        [UnitOfWork]
        public async Task<ResponseResult> PoolTest()
        {
            try
            {
              
                var sys = (await _sysConfigRepository.GetQueryableAsync()).FirstOrDefault();
                sys.ConfigName = "cc";
                var sysList = (await _sysConfigRepository.GetQueryableAsync()).Where(a => a.IsDeleted == false).Select(a=>new { a.Remark,a.ConfigName});

                foreach (var item in sysList) {
                    var menu = new Menu { Name = item.ConfigName };
                    await _menuRepository.InsertAsync(menu);
                    await CurrentUnitOfWork.SaveChangesAsync();
                }
               await CurrentUnitOfWork.SaveChangesAsync();
                return ResponseResult.Success("ok");
            }
            catch (Exception ex)
            {
                return ResponseResult.Fail(ex.Message);
            }
        }

        [HttpPost]
        [AllowAnonymous]
        public  void PoolConnectionTest()
        {

            try
            {
               
                    var dbContext = _sysConfigRepository.GetDbContext();

                    Task.Run(() =>
                    {
                        dbContext.Database.OpenConnection();
                       var  result = dbContext.Database.ExecuteSqlRawAsync("select * from t_sys_config").Result;
                        ///int result = dbContext.Database.ExecuteSqlRawAsync($"INSERT INTO t_sys_config(Id, ConfigName, ConfigKey, ConfigValue, Remark, IsDeleted, TenantId, ExtraProperties, ConcurrencyStamp, CreationTime, CreatorId, LastModificationTime, LastModifierId) VALUES ('{Guid.NewGuid().ToString()}', 'cc', 'bb', 'bb', NULL, 0, NULL, '{{}}', '7e3d8ece3ff9493e9b530ba414cded6c', '2024-06-05 19:57:13', '3a0ed0e8-2ddb-6707-9c2f-32d3f0ae5228', '2024-10-20 13:44:28', NULL);").Result;
                        if (result > 0)
                        {
                            dbContext.SaveChanges();
                        }
                    });
                
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            
        }

    }
}
